"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : restore过程中存在大量未刷盘数据
Create At   : 2020/11/20
@zou_jialiang0505328126
Description :
    1.执行tpcc，检查脏页
    2.恢复数据
    3.检查数据一致性
Expect      :
    1.执行TPCC成功，脏页个数大于2000，若未达到继续执行TPCC
    2.恢复成功
    3.主备数据一致
History     :
    modified: 2020/12/09  modified by @wan005,修改恢复数据库为新创建数据库,
              防止恢复失败导致后续用例失败
    modified: 2021/01/08  modified by @wan005,物化视图不支持compression等参数，
              修改断言适配1.1.0版本
    modified: 2021/07/05  modified by @wan005,创建前增加删除数据库,确保创建成功
    modified: 2023/05/23  modified by @kyeleze,修改bgwriter_delay参数,
              延迟刷盘时间，确保脏页数足够大
"""

import os
import unittest
import time

from yat.test import macro
from yat.test import Node

from testcase.utils.CommonSH import RestartDbCluster
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class Basebackup(unittest.TestCase):
    commonshsta = CommonSH('Standby1DbUser')
    commonshsta2 = CommonSH('Standby2DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info(
            '----Opengauss_Reliability_Tools_Restore_Case001.py 开始执行----')
        self.dbPrimaryDbUser = Node(node='PrimaryDbUser')
        self.dbPrimaryRoot = Node(node='PrimaryRoot')
        self.dbStandby1DbUser = Node(node='Standby1DbUser')
        self.dbStandby2DbUser = Node(node='Standby2DbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.Constant = Constant()
        self.common = Common('PrimaryRoot')
        self.restore_file = os.path.join(
            macro.DB_BACKUP_PATH, 'dump_backup', 'tools_restore_case001.tar')
        self.sql_path = os.path.join(macro.DB_BACKUP_PATH, 'testscript')

        status = Primary_SH.getDbClusterStatus()
        self.log.info(status)
        self.assertTrue("Degraded" in status or "Normal" in status)

        self.log.info('----创建备份路径----')
        shell_cmd = f'rm -rf {os.path.dirname(self.restore_file)};' \
                    f'mkdir {os.path.dirname(self.restore_file)}'
        self.log.info(shell_cmd)
        result = self.dbPrimaryDbUser.sh(shell_cmd).result()
        self.log.info(result)

        self.log.info('----生成预设数据----')
        result = self.common.scp_file(
            self.dbPrimaryDbUser, 'restore_data.sql', self.sql_path)
        self.log.info(result)
        sql_file_cmd = f"source {macro.DB_ENV_PATH};" \
                       f"gsql -d {self.dbPrimaryDbUser.db_name} " \
                       f"-p {self.dbPrimaryDbUser.db_port} " \
                       f"-f {os.path.join(self.sql_path, 'restore_data.sql')}"
        self.log.info(sql_file_cmd)
        sql_bx_msg = self.dbPrimaryDbUser.sh(sql_file_cmd).result()
        self.log.info(sql_bx_msg)
        self.assertNotIn('ERROR', sql_bx_msg)

        self.table_list = Primary_SH.executDbSql(r'\d')
        self.log.info(self.table_list)

        self.log.info('----导出数据----')
        result = Primary_SH.dumpFile(filename=self.restore_file,
                                     get_detail=False)
        self.log.info(result)
        self.assertTrue(result)
        self.log.info('----查询bgwriter_delay默认值----')
        show_msg = self.pri_sh.executDbSql(f"show bgwriter_delay;")
        self.log.info(show_msg)
        self.bgwriter_delay_default = show_msg.splitlines()[-2].strip()

        self.dbname = 'dbsys_restore_db'
        self.log.info("---------------------创建数据库--------------------")
        result = Primary_SH.executDbSql(f'drop database '
            f'if exists {self.dbname};'
            f'create database {self.dbname};')
        self.log.info(result)
        self.assertIn(self.Constant.CREATE_DATABASE_SUCCESS_MSG, result)

    def test_basebackup(self):
        self.log.info('-----设置bgwriter_delay为10s-----')
        result = self.pri_sh.executeGsguc('reload',
                                          self.Constant.GSGUC_SUCCESS_MSG,
                                          'bgwriter_delay=10s')
        self.assertTrue(result, '设置参数失败')
        show_msg = self.pri_sh.executDbSql(f"show bgwriter_delay;")
        self.log.info(show_msg)
        self.assertIn('10s', show_msg, '设置参数失败')
        self.log.info('----启动TPCC----')
        result = self.common.startTPCC(self.dbPrimaryRoot, macro.TPCC_PATH)
        self.log.info(result)
        self.assertIn(self.Constant.TPCC_SUCCESS_MSG, result)

        self.log.info('----获取脏页个数----')
        dirty_page_num = Primary_SH.get_dirty_page_num()
        if 2000 > int(dirty_page_num):
            for i in range(10):
                self.log.info(f'----不存在脏页，再次执行TPCC {i}----')
                result = self.common.startTPCC(
                    self.dbPrimaryRoot, macro.TPCC_PATH)
                self.log.info(result)
                self.assertIn(self.Constant.TPCC_SUCCESS_MSG, result)
                self.log.info('----获取脏页个数----')
                dirty_page_num = Primary_SH.get_dirty_page_num()
                if int(dirty_page_num) > 2000:
                    break
        self.assertLess(2000, int(dirty_page_num))

        self.log.info('----恢复数据----')
        result = Primary_SH.restoreFile(self.restore_file,
                                        get_detail=True,
                                        dbname=self.dbname)
        self.assertIn(self.Constant.RESTORE_SUCCESS_MSG, result)

        self.log.info('----对比恢复前后数据是否一致----')
        time.sleep(5)
        self.table_list_after = Primary_SH.executDbSql(r'\d',
                                                       dbname=self.dbname)
        self.log.info(self.table_list_after)
        self.assertIn(self.table_list, self.table_list_after)

        self.log.info('----检查主备是否同步----')
        result = self.commonshsta.check_data_consistency()
        self.assertTrue(result)
        result = self.commonshsta2.check_data_consistency()
        self.assertTrue(result)

        self.log.info('----检查数据一致性----')
        nodes_tuple = (
            self.dbPrimaryDbUser, self.dbStandby1DbUser, self.dbStandby2DbUser)
        flag = self.common.check_data_sample_by_all(r'\d', *nodes_tuple)
        self.assertTrue(flag)

        table_dict = self.common.format_sql_result(
            Primary_SH.executDbSql(r'\d'))
        table_name = table_dict.get('Name')
        for name in table_name:
            select_sql = f'select count(*) from {name};'
            flag = self.common.check_data_sample_by_all(select_sql,
                                                        *nodes_tuple)
            self.assertTrue(flag)

    def tearDown(self):
        self.log.info('----环境清理----')
        self.log.info('----删除备份文件----')
        shell_cmd = f'rm -rf {os.path.dirname(self.restore_file)}'
        self.log.info(shell_cmd)
        result = self.dbPrimaryDbUser.sh(shell_cmd).result()
        self.log.info(result)

        self.log.info('----删除预设数据----')
        self.common.scp_file(
            self.dbPrimaryDbUser, 'clear_restore_data.sql', self.sql_path)
        sql_file_cmd = \
            f"source {macro.DB_ENV_PATH};" \
            f"gsql -d {self.dbPrimaryDbUser.db_name} " \
            f"-p {self.dbPrimaryDbUser.db_port} " \
            f"-f {os.path.join(self.sql_path, 'clear_restore_data.sql')}"
        self.log.info(sql_file_cmd)
        sql_bx_msg = self.dbPrimaryDbUser.sh(sql_file_cmd).result()
        self.log.info(sql_bx_msg)

        self.log.info('-----删除执行脚本路径----')
        shell_cmd = f'rm -rf {self.sql_path}'
        self.log.info(shell_cmd)
        result = self.dbPrimaryDbUser.sh(shell_cmd).result()
        self.log.info(result)

        self.log.info("----drop database-----")
        result = Primary_SH.executDbSql(f'drop database {self.dbname};')
        self.log.info(result)
        self.log.info('-----恢复bgwriter_delay-----')
        guc_result = self.pri_sh.executeGsguc(
            'reload', self.Constant.GSGUC_SUCCESS_MSG,
            f'bgwriter_delay={self.bgwriter_delay_default}')
        show_msg = self.pri_sh.executDbSql(f"show bgwriter_delay;")
        self.log.info(show_msg)
        self.assertTrue(guc_result, '恢复参数失败')
        self.assertEqual(self.bgwriter_delay_default,
                          show_msg.splitlines()[-2].strip(), '恢复参数失败')
        self.log.info(
            '----Opengauss_Reliability_Tools_Restore_Case001.py 执行结束----')

